import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.io.IOException;

import javax.imageio.ImageIO;


public class Rocket extends Entity{
	
	private double rx=0;
	private double ry=0;
	private double speed=0;
	private double a=0,b=1;
	private Image sprite;
	private double theta=0;
	AffineTransform affineTransform = new AffineTransform();
	AffineTransform affineTransform2;
	
	public Rocket(){
		
		try {
			sprite = ImageIO.read(getClass().getResource("rocket.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		rx=0;
		ry=0;
		speed=0;
	}

	@Override
	public void draw(Graphics g) {

		Graphics2D g2 = (Graphics2D) g;
		affineTransform2 = g2.getTransform();
		affineTransform.rotate(theta, rx, ry);
		g2.setTransform(affineTransform);
		g2.drawImage(sprite, (int) rx, (int) ry, null);
		affineTransform.rotate(-theta, rx, ry);
		g2.setTransform(affineTransform);
		g2.setTransform(affineTransform2);
	}

	@Override
	public void move() {
		// TODO Auto-generated method stub
		rx-=a*speed;
		ry-=b*speed;
	}

	@Override
	public void move(double dx, double dy) {
	}

	@Override
	public void setX(double x) {
		// TODO Auto-generated method stub
		rx=x;
		
	}

	@Override
	public void setY(double y) {
		// TODO Auto-generated method stub
		ry=y;
	}
	
	public void setVect(double dx, double dy){
		
		double a1 = a;
		double b1 = b;
		double x = (dx-rx)/Math.pow(Math.pow((dx-rx),2)+Math.pow((dy-ry),2),0.5);
		double y =(dy-ry)/Math.pow(Math.pow((dx-rx),2)+Math.pow((dy-ry),2),0.5);
		
		if ((Math.atan2(y,x)-Math.atan2(b1,a1)) > -Math.PI/1800 && (Math.atan2(y,x)-Math.atan2(b1,a1)) < Math.PI/1800) {
			a=(dx-rx)/Math.pow(Math.pow((dx-rx),2)+Math.pow((dy-ry),2),0.5);
			b=(dy-ry)/Math.pow(Math.pow((dx-rx),2)+Math.pow((dy-ry),2),0.5);	
			theta += (Math.atan2(b,a)-Math.atan2(b1,a1));
		}
		
		else {
			if (Math.atan2(y,x)-Math.atan2(b1,a1) > 0 && Math.atan2(y,x)-Math.atan2(b1,a1) < Math.PI) {
				a = a1*Math.cos(Math.PI/1800) - b1*Math.sin(Math.PI/1800);
				b = a1*Math.sin(Math.PI/1800) + b1*Math.cos(Math.PI/1800);
				theta += (Math.atan2(b,a)-Math.atan2(b1,a1));
			}
			else if (Math.atan2(y,x)-Math.atan2(b1,a1) < 0 && Math.atan2(y,x)-Math.atan2(b1,a1) > -Math.PI) {
				a = a1*Math.cos(-Math.PI/1800) - b1*Math.sin(-Math.PI/1800);
				b = a1*Math.sin(-Math.PI/1800) + b1*Math.cos(-Math.PI/1800);
				theta += (Math.atan2(b,a)-Math.atan2(b1,a1));
			}
			else if (Math.atan2(y,x)-Math.atan2(b1,a1) < -Math.PI) {
				a = a1*Math.cos(Math.PI/1800) - b1*Math.sin(Math.PI/1800);
				b = a1*Math.sin(Math.PI/1800) + b1*Math.cos(Math.PI/1800);
				theta += (Math.atan2(b,a)-Math.atan2(b1,a1));
			}
			else if (Math.atan2(y,x)-Math.atan2(b1,a1) > Math.PI) {
				a = a1*Math.cos(-Math.PI/1800) - b1*Math.sin(-Math.PI/1800);
				b = a1*Math.sin(-Math.PI/1800) + b1*Math.cos(-Math.PI/1800);
				theta += (Math.atan2(b,a)-Math.atan2(b1,a1));
			}
		}
		
	}
	
	public void setSpeed(double speedy) {
		// TODO Auto-generated method stub
		speed=speedy;
	}

	@Override
	public double getX() {
		return rx;
		// TODO Auto-generated method stub
		
	}
	
	public Rectangle getBounds(){
		return new Rectangle((int) rx, (int) ry, sprite.getWidth(null), sprite.getHeight(null));
	}

	@Override
	public double getY() {
		return ry;
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getCadence() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setLife() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getLife() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean getFin() {
		// TODO Auto-generated method stub
		return false;
	}
	
}
